package com.lsl.musicplayer.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.lsl.musicplayer.R;

import java.util.List;

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder>{
    private List<String> mData; // 数据源

    // 构造函数，初始化数据源
    public MusicListAdapter(List<String> data) {
        mData = data;
    }

    // 创建视图持有者
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.item_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    // 将数据绑定到视图上
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String text = mData.get(position);
        holder.textView.setText(text);
    }

    // 视图持有者类
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_view); // 根据实际情况找到对应的视图
        }
    }

    // 返回数据项数量
    @Override
    public int getItemCount() {
        return mData.size();
    }
}